package com.flashextended.apps.logomaker.beans.workareaobjects
{
	import com.flashextended.apps.logomaker.singleton.AppControl;
	import com.flashextended.apps.logomaker.singleton.Fonts;
	
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	import mx.events.ResizeEvent;
	
	public class TextOnStage extends GenericObject
	{
		public static const TYPE:String = "TEXT";
		private var textfeed:String;
		private var textField:TextField;
		private var textFormat:TextFormat;
		
		private var sizeOfFont:int = 20;
		private var boldOfFont:Boolean = false;
		private var colorOfFont:uint = 0x000000;
		private var alignOfFont:String = TextFormatAlign.LEFT;
		private var fontUsed:int = 0;
		private var italicOfFont:Boolean = false;
		private var underlineOfFont:Boolean = false;
		private var _isFocus:Boolean = false;
		
		private static const WDT:Number = 130;
		private static const HGT:Number = 70;	
		
		public function TextOnStage(textfeed:String)
		{
			super();
			this.type = TYPE;
			this.textfeed = textfeed;
			this.objectWidth = WDT;
			this.objectHeight = HGT;
			this.doubleClickEnabled = true;
			applyNewTextFormat(fontUsed);
			isFocus = false;
		}
		
		//SETTER & GETTER
		public function set fontType(val:int):void
		{
			fontUsed = val;
			applyNewTextFormat(fontUsed);
		}
		public function get fontType():int
		{
			return fontUsed;
		}
		public function set fontColor(col:uint):void
		{
			textFormat.color = col;
			colorOfFont = col;
			applyNewTextFormat(fontUsed);
		}
		public function get fontColor():uint
		{
			return colorOfFont;
		}
		public function set fontSize(no:int):void
		{
			sizeOfFont = no;
			textFormat.size = no;
			applyNewTextFormat(fontUsed);
		}
		public function get fontSize():int
		{
			return sizeOfFont;
		}
		public function set fontAlign(val:String):void
		{
			textFormat.align = val;
			alignOfFont = val;
			applyNewTextFormat(fontUsed);
		}
		public function get fontAlign():String
		{
			return alignOfFont;
		}		
		
		public function set fontBold(val:Boolean):void
		{
			boldOfFont = val;
			applyNewTextFormat(fontUsed);
		}
		public function get fontBold():Boolean
		{
			return textFormat.bold;
		}
		public function set fontItalic(val:Boolean):void
		{
			italicOfFont = val;
			applyNewTextFormat(fontUsed);
		}
		public function get fontItalic():Boolean
		{
			return italicOfFont;
		}
		public function set fontUnderline(val:Boolean):void
		{
			underlineOfFont = val;
			applyNewTextFormat(fontUsed);
		}
		public function get fontUnderline():Boolean
		{
			return underlineOfFont;
		}
		
		public function set isFocus(val:Boolean):void
		{
			_isFocus = val;
			textField.mouseEnabled = val;
			textField.selectable = val;
			textField.border = _isFocus;
			
			try{
			AppControl.getInstance().workarea.getHandler.visible = !_isFocus;
			}catch(e:Error){}
			
			if(_isFocus)
			{
				if((textField!=null) && (textField.stage!=null))
				{
					textField.stage.focus = textField;
				}
			}
			else
			{
				if((textField!=null) && (textField.stage!=null))
				{
					textField.stage.focus = new TextField();
				}
			}			
		}
		public function get isFocus():Boolean
		{
			return _isFocus;
		}				
		
		//BEGIN OPERATIONS
		override protected function moveX():void
		{
			
		}
		override protected function moveY():void
		{
			
		}
		override protected function changeW():void
		{
			this.width = objectWidth;
		}
		override protected function changeH():void
		{
			this.height = objectHeight;
		}
		override protected function resizeContainer(e:ResizeEvent):void
		{
			objectX = this.x;
			objectY = this.y;
			objectRotation = this.rotation;
			objectWidth = this.width;
			objectHeight = this.height;
			textField.width = objectWidth;
			textField.height = objectHeight;			
		}
		
		//BUILD TEXT AND TEXT FORMAT
		private function applyNewTextFormat(fontno:int):void
		{
			var txt_feed:String = textfeed;
			if(textField!=null)
			{
				txt_feed = textField.text;
				this.removeChild(textField);
				textField=null;
			}
			textField = new TextField();
			textField.embedFonts = true;
							
			if(textFormat!=null)
			{
				textFormat = null;
			}
			textFormat = new TextFormat();
			textFormat.bold = boldOfFont;
			textFormat.size = sizeOfFont;
			textFormat.color = colorOfFont;
			textFormat.align = alignOfFont;
			textFormat.italic = italicOfFont;
			textFormat.underline = underlineOfFont;
			
			if((textFormat.bold) && (!textFormat.italic))
			{
				//trace("is bold");
				textFormat.font = Fonts.getInstance().getembededfontBold(fontno);
			}
			else if((!textFormat.bold) && (textFormat.italic))
			{
				//trace("is italic");
				textFormat.font = Fonts.getInstance().getembededfontItalic(fontno);
			}
			else if((textFormat.bold) && (textFormat.italic))
			{
				//trace("is bold and italic");
				textFormat.font = Fonts.getInstance().getembededfontBoldItalic(fontno);
			}
			else if((!textFormat.bold) && (!textFormat.italic))
			{
				//trace("not bold or italic");
				textFormat.font = Fonts.getInstance().getembededfont(fontno);
			}
			textField.defaultTextFormat = textFormat;
			
			textField.text = txt_feed;
			textField.multiline = true;
			textField.wordWrap = true;
			textField.type = TextFieldType.INPUT;
			textField.width = this.width;
			textField.height = this.height;

			this.addChild(textField);
			this.addEventListener(ResizeEvent.RESIZE, resizeContainer);
		}
		
		public function getTextFeed():String
		{
			return textField.text;
		}
				
		
	}
}